package cn.mumzone.pos.presenter;

import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.mvp.AbsPresenter;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.net.source.AbsRepository;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.model.BillDetailEntity;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.SettleItemEntity;
import cn.mumzone.pos.model.source.BillsRepository;
import cn.mumzone.pos.model.source.MemberRepository;
import cn.mumzone.pos.presenter.contract.BillDetailContract;
import cn.mumzone.pos.util.BillUtil;

/**
 * Created By Dlx
 * on 2019/6/26
 */
public class BillDetailPresenter extends AbsPresenter<BillDetailContract.View, BillsRepository> implements BillDetailContract.Presenter {

    MemberRepository memberRepository;

    @Override
    protected void initOtherRepository() {
        super.initOtherRepository();
        memberRepository = new MemberRepository();
    }

    @Override
    public void getGoodsBillDetail(String saleId, String orderType) {
        mView.showLoadingDialog();
        ApiCallBack<BillDetailEntity> callBack = new ApiCallBack<BillDetailEntity>() {
            @Override
            public void onSuccess(BillDetailEntity billDetailEntity) {
                mView.dismissLoadingDialog();
                mView.onDataReceived(billDetailEntity);
                if(!StringUtils.isEmpty(billDetailEntity.getMemberPhone())){
                    refreshMember(billDetailEntity.getMemberPhone());
                }
                List<SettleItemEntity> settleList = new ArrayList<>();

                //7-17 去掉总价
//                if (billDetailEntity.getOrderType().equals("0")) {
//                    settleList.add(new SettleItemEntity("总计", BillUtil.getServiceOrderOriginSumAmount(billDetailEntity.getList()), SettleItemEntity.TYPE_TITLE));
//                } else if (billDetailEntity.getOrderType().equals("1")) {
//                    settleList.add(new SettleItemEntity("总计", BillUtil.getGoodsOrderOriginSumAmount(billDetailEntity.getGoodsList()), SettleItemEntity.TYPE_TITLE));
//                }

                // 没有议价折扣和会员折扣了，新增了赠送金额
//                settleList.add(new SettleItemEntity("议价折扣", billDetailEntity.getBargainingAmount(), SettleItemEntity.TYPE_ITEM));
//                settleList.add(new SettleItemEntity("会员折扣", billDetailEntity.getMemberDiscount(), SettleItemEntity.TYPE_ITEM));
//                settleList.add(new SettleItemEntity("赠送金额", billDetailEntity.getGiftAmount(), SettleItemEntity.TYPE_ITEM));

                if (StringUtils.isNumber(BigDecimalUtil.getNumber(billDetailEntity.getMlPrice()))
                        && Double.parseDouble(BigDecimalUtil.getNumber(billDetailEntity.getMlPrice())) > 0) {
                    settleList.add(new SettleItemEntity("抹零", "-" + StringUtils.getFormattedMoneyWithUnit(billDetailEntity.getMlPrice()), SettleItemEntity.TYPE_ITEM));
                }

                if (!ListUtil.isListEmpty(billDetailEntity.getDiscountList())) {
                    for (BillDetailEntity.DiscountItemBean bean : billDetailEntity.getDiscountList()) {
                        settleList.add(new SettleItemEntity(StringUtils.get(bean.getName()), "-" + StringUtils.getFormattedMoneyWithUnit(bean.getAmount()), SettleItemEntity.TYPE_ITEM));
                    }
                }


                String payType = billDetailEntity.getPayType();
                JSONObject jsonObject = JSONObject.parseObject(payType);
                for (String key : jsonObject.keySet()) {
                    if (key.equals("3")) {//现金
                        settleList.add(new SettleItemEntity("现金", StringUtils.getFormattedMoneyWithUnit(billDetailEntity.getCashCollection()), SettleItemEntity.TYPE_TITLE));
                        if (StringUtils.isNumber(billDetailEntity.getCashChange()) && Double.parseDouble(billDetailEntity.getCashChange()) > 0) {
                            settleList.add(new SettleItemEntity("找零", StringUtils.getFormattedMoneyWithUnit(billDetailEntity.getCashChange()), SettleItemEntity.TYPE_TITLE));
                        }
                    } else {
                        if ("7".equals(key) && "3".equals(billDetailEntity.getOrderType())) {
                            settleList.add(new SettleItemEntity("积分支付", String.format("%s积分", StringUtils.get((String) jsonObject.get(key))),
                                    SettleItemEntity.TYPE_TITLE));
                        } else if ("7".equals(key)) {
                            settleList.add(new SettleItemEntity(!StringUtils.isEmpty(billDetailEntity.getIntegralMoney()) ?
                                    String.format("%s积分抵现", BigDecimalUtil.getNumber(billDetailEntity.getIntegralMoney())) : BillUtil.getSimplePayType(key)
                                    , StringUtils.getFormattedMoneyWithUnit((String) jsonObject.get(key)), SettleItemEntity.TYPE_TITLE));
                        } else {
                            settleList.add(new SettleItemEntity(BillUtil.getSimplePayType(key), StringUtils.getFormattedMoneyWithUnit((String) jsonObject.get(key)), SettleItemEntity.TYPE_TITLE));
                        }
                    }
                }

                if (!ListUtil.isListEmpty(billDetailEntity.getTimesCardList())) {
                    for (BillDetailEntity.DiscountItemBean bean : billDetailEntity.getTimesCardList()) {
                        settleList.add(new SettleItemEntity(StringUtils.get(bean.getName()), StringUtils.getFormattedMoneyWithUnit(bean.getAmount()), SettleItemEntity.TYPE_TITLE));
                    }
                }


                double returnAmount = 0.0;
                if (!StringUtils.isEmpty(billDetailEntity.getReturnMoney())) {
                    returnAmount = Double.parseDouble(billDetailEntity.getReturnMoney());
                }
//                if (!"0".equals(billDetailEntity.getOrderType())) {
//                    if (!"0".equals(billDetailEntity.getReturnMoney()) && !StringUtils.isEmpty(billDetailEntity.getReturnMoney())) {
//                        settleList.add(new SettleItemEntity("退货", billDetailEntity.getReturnMoney(), SettleItemEntity.TYPE_ITEM));
//                    }
//                }
                String returnPay = billDetailEntity.getMixReturnPay();
                if (!StringUtils.isEmpty(returnPay)) {
                    JSONObject returnPayObj = JSONObject.parseObject(returnPay);
                    if (!returnPayObj.isEmpty()) {
                        double returnMoney = 0.0;
                        for (String key : returnPayObj.keySet()) {
                            returnMoney = BigDecimalUtil.add(String.valueOf(returnMoney), (String) returnPayObj.get(key));
                        }

//                        if (returnPayObj.size() > 1) {
                        settleList.add(new SettleItemEntity("退货", "", SettleItemEntity.TYPE_ITEM));
                        for (String key : returnPayObj.keySet()) {
                            if ("7".equals(key)) {

                                settleList.add(new SettleItemEntity(BillUtil.getSimplePayType(key), "不退还", SettleItemEntity.TYPE_ITEM));
                            } else {
                                settleList.add(new SettleItemEntity(BillUtil.getSimplePayType(key) + "退货", "-" + StringUtils.getFormattedMoneyWithUnit((String) returnPayObj.get(key)), SettleItemEntity.TYPE_ITEM));

                            }
                        }
//                        } else {
//                            settleList.add(new SettleItemEntity("退货", "-" + StringUtils.getFormattedMoneyWithUnit(String.valueOf(returnMoney)), SettleItemEntity.TYPE_ITEM));
//                        }
                    }
                }
                mView.onBillSettleDetail(settleList);

            }

            @Override
            public void onError(BaseResponse response) {
                mView.dismissLoadingDialog();
                mView.showToast(response.getMsg());
            }
        };
        mRepo.getBillDetail(saleId, orderType, callBack);
        addSubscrebe(callBack);
    }


    private void refreshMember(String phone){
        ApiCallBack<MemberEntity> callBack = new ApiCallBack<MemberEntity>() {
            @Override
            public void onSuccess(MemberEntity memberEntity) {
                memberEntity.setId(memberEntity.getMemberId());
                mView.showRefreshMember(memberEntity);
            }

            @Override
            public void onError(BaseResponse response) {
            }

            @Override
            public void onNetworkError(BaseResponse response) {

            }
        };
        memberRepository.getMemberInfoByPhone(phone, callBack);
    }
}
